import 'package:flutter/material.dart';
import 'loading_dialog.dart';

class DialogManager {
  static LoadingDialog dialog;

  static showLoadingDialog(
      {@required BuildContext context,
      String text = "Loading...",
      bool dismissOnBackPressed = true}) {
    if (dialog != null) {
      return;
    }
    showDialog(
        context: context,
        barrierDismissible: false,
        builder: (buildContext) {
          dialog = LoadingDialog(
              buildContext: buildContext,
              text: "$text",
              dismissOnBackPressed: dismissOnBackPressed);
          return dialog;
        });
  }

  static resetLoadingDialog() {
    if (dialog != null) {
      dialog = null;
    }
  }

  static dismissLoadingDialog() {
    WidgetsBinding.instance.addPostFrameCallback((Duration timeStamp){
      if (dialog != null) {
        dialog.dismissDialog();
        dialog = null;
      }
    });
  }
}
